<?php

namespace Wzbmail\Yii2Api\response\wrapper;

use yii\base\Model;

trait ApiWrapperTrait
{
    public function apiWrapper()
    {
        return new ApiWrapper();
    }

    public function modelError(Model $model): ApiWrapper
    {
        $errors = $model->getErrorSummary(true);
        return $this->apiWrapper()
            ->error(empty($errors) ? \Yii::t('app', 'Load data error.') : $errors[0],
                ['errors' => $model->getErrors()]);
    }
}